package com.fsc.app_news;

import java.util.ArrayList;
import java.util.List;

import com.fsc.app_bean.Image;
import com.fsc.app_bean.Title;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class HomeActivity extends Fragment implements OnItemClickListener{
	private GridView gv_Title;
	private View view;
	private int colnum=1;
	private static List<Title> data;
	private static Title title;
	private GrAdapter adapter;
	private ImageView iv_play;
	
	private ListView lv_newContent;
	private Image newContent;
	private List<Image> list;
	private ListAdapter listadapter;
	
	 
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.activity_home,container,false);
		init();
		envet();
		//准备集合数据
		data = new ArrayList<Title>();
		data.add(new Title("新闻"));data.add(new Title("财经"));data.add(new Title("娱乐"));
		data.add(new Title("体育"));data.add(new Title("社会"));data.add(new Title("星座"));
		
		data.add(new Title("视频"));data.add(new Title("汽车"));data.add(new Title("女性"));
		data.add(new Title("NBA"));data.add(new Title("军事"));data.add(new Title("科技"));
		
		data.add(new Title("图片"));data.add(new Title("笑话"));data.add(new Title("本地"));
		data.add(new Title("购物"));data.add(new Title("旅游"));data.add(new Title("买房"));
		
		data.add(new Title("游戏"));data.add(new Title("空间"));data.add(new Title("书城"));
		data.add(new Title("书城"));data.add(new Title("新浪"));data.add(new Title("网易"));
		
		adapter = new GrAdapter(getActivity());
		gv_Title.setAdapter(adapter);
		
		/*
		 * 设置ListView
		 */
		list = new ArrayList<Image>();
		list.add(new Image("与每天天不亮就要起床往学校赶的大多数中国孩子不同，"
				+ "他们“在家上学”。一年级退学，除了中间短暂复课，于笑笑已经在家学习7年。"
				+ "韩百川初一离校，也已回家自学5年。",1,R.drawable.tu1,null));
		list.add(new Image("对他们和他们的父母而言，家的意义远远不只是休憩的港湾，"
				+ "更是一个探索新教育方法的乌托邦。",2,R.drawable.tu2,null));
		list.add(new Image("韩百川的决定很突然，刚上初一不久，他就向家人提出想要回家学习。"
				+ "他的理由只有一句话：“学校作业太多，我没有时间做自己喜欢的事”，他喜欢的事是中学教育中没有、"
				+ "高考也不会涉及的绘画和编程。",3,R.drawable.tu3,null));
		list.add(new Image("母亲听到以后的第一反应是反对，“学习好好的，为什么要退学？”韩百川从小成绩优异，"
				+ "小学三四年级曾考过年级第一名，所以即使身处高考大省山东，"
				+ "妈妈也从来没有怀疑过儿子能够考进一所不错的大学",4,R.drawable.tu4,null));
		list.add(new Image("韩百川的妈妈由爷爷奶奶带大，娇生惯养，从戏曲中学毕业以后就进入社会工作。"
				+ "她对儿子的管束一向宽松，韩百川从小都是自己挑衣服。"
				+ "“父母有了孩子其实就是大孩子带着小孩子一起过日子，都要成长。”她谈吐温文尔雅，语气谦和。",5,R.drawable.tu1,null));
		listadapter = new ListAdapter(getActivity());
		lv_newContent.setAdapter(listadapter);
		return view;
	}
	public void init(){
		 gv_Title = (GridView) view.findViewById(R.id.gv_Title);
		 colnum = (int) (((getResources().getDisplayMetrics().widthPixels)) / 120);
		 gv_Title.setNumColumns(colnum);
		 
		 lv_newContent = (ListView) view.findViewById(R.id.lv_newContent);
		 iv_play = (ImageView) view.findViewById(R.id.iv_play);
	}
	public void envet(){
		gv_Title.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(getActivity(),HtcActivity.class);
				title = data.get(position);
				intent.putExtra("HTC", title.getTitle());
				startActivity(intent);
			}
		});
		
		//新闻页
		lv_newContent.setOnItemClickListener(this);
	}
	class GrAdapter extends BaseAdapter{
  		private Context context;
  		//构造函数
  		public GrAdapter(Context context) {  
  	        this.context = context;
  	    }
  		@Override
  		public int getCount() {
  			return data.size();
  		}

  		@Override
  		public Object getItem(int position) {
  			return data.get(position);
  		}

  		@Override
  		public long getItemId(int position) {
  			return 0;
  		}

  		@Override
  		public View getView(int position, View convertView, ViewGroup parent) {
  			
  			ViewHolder hodler = null; 
  	        if(convertView == null)  
  	        {  
  	        	hodler = new ViewHolder();
  	            convertView = LayoutInflater.from(context).inflate(R.layout.item_title, null); 
  	            hodler.title = (TextView) convertView.findViewById(R.id.tv_title);   
  	            convertView.setTag(hodler);         //表示给View添加一个格外的数据，  
  	        } else {  
  	        	hodler = (ViewHolder) convertView.getTag();//通过getTag的方法将数据取出来  
  	        } 
  	      title = data.get(position); 
  	        
  	        hodler.title.setText(title.getTitle());        //显示标题    
  	        return convertView; 
        }
    }
	//ListView的适配器
	class ListAdapter extends BaseAdapter{
		private Context context;
  		//构造函数
  		public ListAdapter(Context context) {  
  	        this.context = context;
  	    }
		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder hodler = null; 
  	        if(convertView == null)  
  	        {  
  	        	hodler = new ViewHolder();
  	            convertView = LayoutInflater.from(context).inflate(R.layout.item_content, null); 
  	            hodler.content = (TextView) convertView.findViewById(R.id.tv_content);   
  	            hodler.image = (ImageView) convertView.findViewById(R.id.iv_image);
  	            convertView.setTag(hodler);         //表示给View添加一个格外的数据，  
  	        } else {  
  	        	hodler = (ViewHolder) convertView.getTag();//通过getTag的方法将数据取出来  
  	        } 
  	        newContent = list.get(position); 
  	        
  	        hodler.content.setText(newContent.getContent());//内容   
  	        hodler.image.setImageResource(newContent.getImage());//图片  
  	        return convertView;
		}
		
	}
	public class ViewHolder {
		private TextView title;
		private TextView content;
		private ImageView image;
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent Listintent = new Intent(getActivity(),NewPageActivity.class);
		newContent = list.get(position); 
		
		Listintent.putExtra("Title",newContent.getContent());
		Listintent.putExtra("Content",newContent.getContent());
		startActivity(Listintent); 
	}
}
